#import "YQPCategoryModel.h"

#import "MYHomeSubclassVC.h"
#import <JXCategoryView/JXCategoryView.h>
#import "MYHomeSubclassListVC.h"
@interface MYHomeSubclassVC ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
@property (nonatomic, strong) JXCategoryListContainerView * listContainerView;
@property (nonatomic, strong) NSMutableArray * titles;

@property (nonatomic, strong) NSMutableArray * titlesArr;

@end

@implementation MYHomeSubclassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titles = NSMutableArray.new;
    
    self.titlesArr = NSMutableArray.new;

    [self getUserHomeSubTitlesApi];
}

#pragma mark - 获取首页子级标题
- (void)getUserHomeSubTitlesApi
{
    [YQPNetWorkingManager sendPostRequest:kPOST_INVITATION_CATEGORY_LIST_MANAGER_URL parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        NSArray *titles = [YQPCategoryModel mj_objectArrayWithKeyValuesArray:responseObject.data];
        [self.titlesArr addObjectsFromArray:titles];
        for (YQPCategoryModel *model in titles) {
            if (model.name) {
                [self.titles addObject:kYQLS(model.name)];
            }
        }
        
        self.categoryView = [[JXCategoryTitleView alloc] init];
        self.categoryView.delegate = self;
        self.categoryView.titles = self.titles;
        self.categoryView.defaultSelectedIndex = 0;
        //    self.categoryView.cellSpacing = 82;
        self.categoryView.titleSelectedColor = HexColor(0x79C4C7);
        self.categoryView.titleColor = HexColor(0xA0A0A0);
        self.categoryView.titleFont = kSystemMedium(12);
        self.categoryView.titleSelectedFont = kSystemMedium(12);
        self.categoryView.contentEdgeInsetLeft = 12;
        self.categoryView.backgroundColor = UIColor.whiteColor;
        self.categoryView.titleColorGradientEnabled = YES;
    //    self.categoryView.titleLabelZoomEnabled = YES;
        self.categoryView.averageCellSpacingEnabled = YES;
        
        [self.view addSubview:self.categoryView];
        
        self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
        self.listContainerView.defaultSelectedIndex = 0;
        [self.view addSubview:self.listContainerView];
        
        self.categoryView.contentScrollView = self.listContainerView.scrollView;
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}

-(void)setupView {
    [super setupView];
    
//    self.titles = @[kYQLS(@"家居与睡眠"),kYQLS(@"玩具与教育"),kYQLS(@"哺育产品"),kYQLS(@"幼儿健康"),kYQLS(@"旅行/出门")];

}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.categoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    self.listContainerView.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height - 44);
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}
#pragma mark - JXCategoryViewDelegate end
#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    YQPCategoryModel *model = self.titlesArr[index];
    MYHomeSubclassListVC *listVC = [[MYHomeSubclassListVC alloc] init];
    listVC.type = self.type;
    listVC.channelId = model.channelId;//[NSString stringWithFormat:@"%ld",(long)index + 1];
    [self addChildViewController:listVC];
    return listVC;
//    if (index == 0) {
//        YQPWaaitToBeDoneLateListVC *lateVC = [[YQPWaaitToBeDoneLateListVC alloc] init];
//        [self addChildViewController:lateVC];
//        return lateVC;
//    }else if(index == 1){
//        YQPWaaitToBeDoneScheduleListVC *scheduleVC = [[YQPWaaitToBeDoneScheduleListVC alloc] init];
//        [self addChildViewController:scheduleVC];
//        return scheduleVC;
//    }else{
//        YQPWaaitToBeDoneAllListVC *allVC = [[YQPWaaitToBeDoneAllListVC alloc] init];
//        [self addChildViewController:allVC];
//        return allVC;
//    }
    //    YQPWaaitToBeDoneListVC *listVC = [[YQPWaaitToBeDoneListVC alloc] init];
    //    //listVC.naviController = self.navigationController;
    //    //如果列表是UIViewController包裹的，需要添加addChildViewController代码，这样子在iPhoneX系列手机就不会有底部安全距离错误的问题！！！
    //    [self addChildViewController:listVC];
    //    return listVC;
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

#pragma mark - JXCategoryListContainerViewDelegate end

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

#pragma mark - JXCategoryListContentViewDelegate end

@end
